<?php

class BingPr{

   /* Function to get ranking position of domain for given keyword.
    * var $keyword [string]
    * var $domain [string] | www.domain.com | domain.com
    * Whichever you expect your domain to be listed with or without www prefix.
    */
    public function bingrank($keyword,$domain){

	$rank = '0';
	$urls = array();

	for($i=0;$i<5;$i++){
		$rn=$i=='0'?'':$i;
		$query = "http://www.bing.com/search?q=".urlencode($keyword)."&first=".$rn."1";
		$result_page = file_get_contents($query);
		preg_match_all('/class="sb_tlst"><h3><a href="(.*?)"/',$result_page,$matches);
		foreach($matches[1] AS $url){
			$urls[] = $url;
		}
	}

	for($i=0;$i< count($urls);$i++){
		if(stristr($urls[$i],'http://'.$domain)){
			$rank = $i +1;
			break;
		}
	}
	return $rank;

    }

    // return page indexed from bing
    public function bingIndexed($domain)
    {
        $query = "http://www.bing.com/search?q=site:".$domain;
        $result_page = file_get_contents($query);
        preg_match_all('/class="sb_tlst"><h3><a href="(.*?)"/',$result_page,$matches);
        foreach($matches[1] AS $url){
            $urls[] = $url;
        }
        return $urls;
    }

}
